package generics;

import net.mindview.util.Generator;

import java.util.Iterator;

/**
 * @author 杨宇
 * @version 1.0 2016/10/27
 */
public class E07_InterableFibonacci {

    public static void main(String[] args) {
        for(int i : new IterableFibonacci(18))
            System.out.print(i + " ");
    }
}

class IterableFibonacci implements Iterable<Integer> {
    Fibonacci fib = new Fibonacci();
    int n = 0;

    IterableFibonacci(int n) {
        this.n = n;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {

            @Override
            public boolean hasNext() {
                return n>0;
            }

            @Override
            public Integer next() {
                n--;
                return fib.next();
            }

            @Override
            public void remove() {

            }
        };
    }
}

class Fibonacci implements Generator<Integer> {

    private int count = 0;

    @Override
    public Integer next() {
        return fib(count++);
    }

    private int fib(int n) {
        if (n < 2) return 1;
        return fib(n - 2) + fib(n - 1);
    }

    public static void main(String[] args) {
        Fibonacci gen = new Fibonacci();
        for (int i = 0; i < 18; i++) {
            System.out.println(gen.next() + "");
        }
    }
}
